using Microsoft.AspNetCore.Mvc;
using RestfulApi.Data;
using RestfulApi.Models;

namespace RestfulApi.Controllers
{
    /// <summary>
    /// 图书控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class BooksController : ControllerBase
    {
        private readonly IBookRepository _bookRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bookRepository">图书仓储</param>
        public BooksController(IBookRepository bookRepository)
        {
            _bookRepository = bookRepository;
        }

        /// <summary>
        /// 获取所有图书
        /// </summary>
        /// <returns>图书列表</returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Book>>> GetBooks()
        {
            var books = await _bookRepository.GetAllBooksAsync();
            return Ok(books);
        }

        /// <summary>
        /// 根据ID获取图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>图书对象</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<Book>> GetBook(int id)
        {
            var book = await _bookRepository.GetBookByIdAsync(id);
            if (book == null)
            {
                return NotFound($"图书ID {id} 不存在");
            }
            return Ok(book);
        }

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="book">图书对象</param>
        /// <returns>添加后的图书对象</returns>
        [HttpPost]
        public async Task<ActionResult<Book>> PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var addedBook = await _bookRepository.AddBookAsync(book);
            await _bookRepository.SaveChangesAsync();

            return CreatedAtAction(nameof(GetBook), new { id = addedBook.Id }, addedBook);
        }

        /// <summary>
        /// 更新图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <param name="book">图书对象</param>
        /// <returns>操作结果</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> PutBook(int id, Book book)
        {
            if (id != book.Id)
            {
                return BadRequest("ID不匹配");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var result = await _bookRepository.UpdateBookAsync(book);
            if (!result)
            {
                return NotFound($"图书ID {id} 不存在");
            }

            await _bookRepository.SaveChangesAsync();
            return NoContent();
        }

        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteBook(int id)
        {
            var result = await _bookRepository.DeleteBookAsync(id);
            if (!result)
            {
                return NotFound($"图书ID {id} 不存在");
            }

            await _bookRepository.SaveChangesAsync();
            return NoContent();
        }

        /// <summary>
        /// 根据作者查询图书
        /// </summary>
        /// <param name="author">作者名</param>
        /// <returns>图书列表</returns>
        [HttpGet("author/{author}")]
        public async Task<ActionResult<IEnumerable<Book>>> GetBooksByAuthor(string author)
        {
            var books = await _bookRepository.GetBooksByAuthorAsync(author);
            return Ok(books);
        }

        /// <summary>
        /// 根据分类查询图书
        /// </summary>
        /// <param name="category">分类名</param>
        /// <returns>图书列表</returns>
        [HttpGet("category/{category}")]
        public async Task<ActionResult<IEnumerable<Book>>> GetBooksByCategory(string category)
        {
            var books = await _bookRepository.GetBooksByCategoryAsync(category);
            return Ok(books);
        }
    }
}